package com.hgkj.cloud.customer.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hgkj.cloud.base.common.PageInfo;
import com.hgkj.cloud.base.domain.SysTestingAnswer;
import com.hgkj.cloud.base.domain.SysTestingQuestion;
import com.hgkj.cloud.base.domain.SysTestingQuestionType;
import com.hgkj.cloud.base.domain.SysTestingScoreStandard;
import com.hgkj.cloud.customer.common.ResponseData;
import com.hgkj.cloud.base.service.SysTestingAnswerService;
import com.hgkj.cloud.base.service.SysTestingQuestionService;
import com.hgkj.cloud.base.service.SysTestingQuestionTypeService;
import com.hgkj.cloud.base.service.SysTestingScoreStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/test")
public class AdminTestController {

    private static final String CUT_OFF_LINE = "separator";

    @Autowired
    private SysTestingQuestionTypeService sysTestingQuestionTypeService;

    @Autowired
    private SysTestingQuestionService sysTestingQuestionService;

    @Autowired
    private SysTestingAnswerService sysTestingAnswerService;

    @Autowired
    private SysTestingScoreStandardService sysTestingScoreStandardService;

    /**
     * 查询列表
     * @param testingParentType
     * @param testingSubType
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping("/datagrid")
    public ResponseData datagrid(@RequestParam(value = "testingParentType", required = false) Integer testingParentType,
                                 @RequestParam(value = "testingSubType", required = false) Integer testingSubType,
                                 String title, Integer page , Integer pageSize) {
        ResponseData responseData = new ResponseData();
        PageInfo pi = new PageInfo();
        pi.setPageSize(pageSize);
        pi.setCurrentPage(page);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("testingParentType", testingParentType);
        map.put("testingSubType", testingSubType);
        map.put("title",title);
        long totalCount = sysTestingQuestionTypeService.getCountForTesting(map);
        List<SysTestingQuestion> list = sysTestingQuestionService.listTesting(map, pi);
        map.clear();
        map.put("list",list);
        map.put("count",totalCount);
        responseData.setData(map);
        return responseData;
    }

    /**
     * 测试题详情
     * @param id
     * @return
     */
    @RequestMapping("/view")
    public ResponseData view(@RequestParam("id") Integer id) {
        ResponseData responseData = new ResponseData();
        SysTestingQuestion question = sysTestingQuestionService.getQuestionById(id.intValue());
        int typeId = question.getTypeId();
        SysTestingQuestionType subQuesType = sysTestingQuestionTypeService.getTestQuestionTypeById(typeId);
        SysTestingQuestionType parentQuesType = sysTestingQuestionTypeService.getTestQuestionTypeById(subQuesType.getParentId());
        List<SysTestingAnswer> alist = sysTestingAnswerService.getAnswers(question.getId());
        Map<String, Object> map = new HashMap();
        map.put("parentQuesType", parentQuesType);
        map.put("subQuesType", subQuesType);
        map.put("question", question);
        map.put("list", alist);
        responseData.setData(map);
        return responseData;
    }

    /**
     * 测试题编辑页
     *
     * @param id
     * @return
     */
    @RequestMapping("/gotoEdit")
    public ResponseData gotoEdit(@RequestParam("id") Integer id) {
        ResponseData responseData = new ResponseData();

        SysTestingQuestion question = sysTestingQuestionService.getQuestionById(id.intValue());
        List<SysTestingAnswer> alist = sysTestingAnswerService.getAnswers(question.getId());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("question", question);
        map.put("ansewer", alist);

        int typeId = question.getTypeId();

        SysTestingQuestionType subQuesType = sysTestingQuestionTypeService.getTestQuestionTypeById(typeId);
        SysTestingQuestionType parentQuesType = sysTestingQuestionTypeService.getTestQuestionTypeById(subQuesType.getParentId());

        map.put("parentQuesType", parentQuesType);
        map.put("subQuesType", subQuesType);

        map.put("questionId", id);
//        List<TestQuestionType> parentList = qaService.getParentQuestionType();
//        map.put("parentTypeList", parentList);
        responseData.setData(map);
        return responseData;
    }

    /**
     * 测试题修改
     *
     * @param ids
     * @param questionId
     * @param testingParentType
     * @param testingSubType
     * @param questionDesc
     * @param questionImageUrl
     * @param answerDescs
     * @param answerImageUrls
     * @param answerScores
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public ResponseData edit(@RequestParam("id") Integer[] ids, @RequestParam("questionId") Integer questionId,
                             @RequestParam("testingParentType") Integer testingParentType,
                             @RequestParam("testingSubType") Integer testingSubType,
                             @RequestParam("questionDesc") String questionDesc,
                             MultipartFile questionImageUrl,
                             @RequestParam("answerDesc") String[] answerDescs,
                             MultipartFile[] answerImageUrls,
                             @RequestParam("answerScore") int[] answerScores) {
        ResponseData responseData = new ResponseData();
        SysTestingQuestion tq = new SysTestingQuestion();
        tq.setTypeId(testingSubType);
        tq.setId(questionId);
        tq.setQuestionDesc(questionDesc);

        List<SysTestingAnswer> alist = new ArrayList<SysTestingAnswer>();
        for (int i = 0; i < answerDescs.length; i++) {
            SysTestingAnswer ta = new SysTestingAnswer();
            try {
                ta.setId(ids[i]);
            } catch (Exception e1) {
            }
            ta.setQuestionId(questionId);
            ta.setAnswerDesc(answerDescs[i]);
            ta.setAnswerScore(answerScores[i]);
            alist.add(ta);
        }

        try {
            sysTestingQuestionService.updateQuestion(tq);
            for (SysTestingAnswer tas : alist) {
                if (tas.getId() != null) {
                    sysTestingAnswerService.updateAnswer(tas);
                } else {
                    sysTestingAnswerService.insertAnswer(tas);
                }

            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            responseData.setCode(500);
            responseData.setMsg("编辑失败");
            e.printStackTrace();
        }
        return responseData;

    }

    /**
     * 删除测试题
     *
     * @param id 测试题id
     * @return
     */
    @RequestMapping("/del")
    @ResponseBody
    public ResponseData del(@RequestParam("id") int id) {
        ResponseData responseData = new ResponseData();
        try {
            sysTestingQuestionService.deleteQuestion(id);
            sysTestingAnswerService.deleteAnswers(id);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return responseData;
    }

    /**
     * 父类型
     * @return
     */
    @RequestMapping("/getSubTypeNoParent")
    public ResponseData getSubTypeNoParent() {
        ResponseData responseData = new ResponseData();
        List<SysTestingQuestionType> childQuestionTypesNoParentId = sysTestingQuestionTypeService.getChildQuestionTypesNoParentId();
        Map map = new HashMap();
        map.put("data",childQuestionTypesNoParentId);
        responseData.setData(map);
        return responseData;
    }

    /**
     * 根据测试题父类型获取子类型
     *
     * @param parentId
     * @return
     */
    @RequestMapping("/getSubType")
    public ResponseData getSubType(@RequestParam("parentId") Integer parentId) {
        ResponseData responseData = new ResponseData();
        Map map = new HashMap();
        map.put("data",sysTestingQuestionTypeService.getChildQuestionType(parentId));
        responseData.setData(map);
        return responseData;
    }

    /**
     * 保存测试题
     *
//     * @param testingSubType
//     * @param questionDescs
//     * @param questionImageUrl
//     * @param answerDescs
//     * @param answerDescs
//     * @param answerScores
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/uploadQuestions", method = RequestMethod.POST)
    public ResponseData uploadQuestions(@RequestBody String params
//            @RequestParam("testingSubType") Integer testingSubType,
//                                        @RequestParam("questionDesc") String[] questionDescs,
//                                        MultipartFile[] questionImageUrl,
//                                        @RequestParam("answerDesc") String[] answerDescs,
//                                        MultipartFile[] answerImageUrl,
//                                        @RequestParam("answerScore") Integer[] answerScores
    )
            throws Exception {
        ResponseData responseData = new ResponseData();
        List qlist = new ArrayList();
        List zList = new ArrayList();
        JSONArray jsonArray = JSONArray.parseArray(params);
        int num = 0;
        for(int i =0;i<jsonArray.size();i++){
            SysTestingQuestion question = new SysTestingQuestion();
            SysTestingAnswer answer = new SysTestingAnswer();
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            if (num == 0){
                question.setTypeId(jsonObject.getInteger("testingSubType"));
                question.setQuestionDesc(jsonObject.getString("questionDesc"));
                sysTestingQuestionService.insertQuestion(question);
                num = question.getId();
            }
            answer.setQuestionId(num);
            answer.setAnswerDesc(jsonObject.getString("answerDesc"));
            answer.setAnswerScore(jsonObject.getInteger("answerScore"));
            sysTestingAnswerService.insertAnswer(answer);
        }
//        qaService.insertQuestionAndAnswers(qlist, zList);
        return responseData;
    }


    /**
     * 保存测试题类型
     *
//     * @param testingParentType
//     * @param testingNames
//     * @param testingImageFiles
//     * @param testingTypeDescs
//     * @param times
     * @return
     * @throws Exception
     */
    @RequestMapping("uploadQuestionType")
    public ResponseData uploadQuestionType(@RequestBody String params)
            throws Exception {
        ResponseData responseData = new ResponseData();
        try {
            JSONArray jsonArray = JSONArray.parseArray(params);
            for(int i = 0; i < jsonArray.size(); i++){
                SysTestingQuestionType tqt = new SysTestingQuestionType();
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                tqt.setTime(jsonObject.getInteger("time"));
                tqt.setParentId(jsonObject.getInteger("type"));
                tqt.setTestingImageUrl(jsonObject.getString("imageUrl"));
                tqt.setTestingName(jsonObject.getString("title"));
                tqt.setTestingTypeDesc(jsonObject.getString("brief"));
                sysTestingQuestionTypeService.insertQuestionType(tqt);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            responseData.setCode(500);
            responseData.setMsg("保存失败");
            e.printStackTrace();
        }
        return responseData;
    }

    /**
     * 保存测试题评分标准
     *
     * @return
     */
    @RequestMapping(value = "/uploadStandard", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData uploadStandard(@RequestBody String params) {
        JSONArray jsonArray = JSONArray.parseArray(params);
        ResponseData responseData = new ResponseData();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            SysTestingScoreStandard s = new SysTestingScoreStandard();
            s.setQuestionTypeId(jsonObject.getInteger("testingSubType"));
            s.setStandardDesc(jsonObject.getString("standardDesc"));
            s.setStandardFrom(jsonObject.getInteger("standardFrom"));
            s.setStandardTo(jsonObject.getInteger("standardTo"));
            sysTestingScoreStandardService.insertStandard(s);
        }
        return responseData;
    }

    /***
     * 后台修改趣味测试的类型 begin
     */
    @RequestMapping("/getAllTest")
    public ResponseData getAllTest(Integer page,Integer pageSize){
        ResponseData responseData = new ResponseData();
        List<Map> allTest = sysTestingQuestionTypeService.getAllTest(page, pageSize);
        com.github.pagehelper.PageInfo pageInfo = new com.github.pagehelper.PageInfo(allTest);
        Map map = new HashMap();
        map.put("data",pageInfo);
        responseData.setData(map);
        return responseData;
    }

    @RequestMapping("/getTestById")
    public ResponseData getAllTest(@RequestParam("test_id") Long test_id){
        ResponseData responseData = new ResponseData();
        Map testById = sysTestingQuestionTypeService.getTestById(test_id);
        responseData.setData(testById);
        return responseData;
    }

    @RequestMapping("/updateTestType")
    public ResponseData updateTestType(@RequestParam("test_id") Long test_id,
                                       @RequestParam("type_id") Long type_id,
                                       @RequestParam("question") String question,
                                       @RequestParam("questionDesc")String questionDesc){
        Map map = new HashMap();
        map.put("test_id",test_id);
        map.put("type_id",type_id);
        map.put("questionDesc",questionDesc);
        map.put("question",question);
        sysTestingQuestionTypeService.updatetest(map);
        return new ResponseData();
    }

}
